package at.fhooe.mc.msports;

import java.util.Vector;

import net.rim.device.api.database.Cursor;
import net.rim.device.api.database.Database;
import net.rim.device.api.database.Row;
import net.rim.device.api.database.Statement;

public final class SessionManager {

	private static Database database;
	
	public static void setDatabase(Database db){
		database = db;
	}
	
	public static Vector claimLastWeeksSessions(){
		Cursor c;
		Vector sessions = new Vector();
		
		try{
			Statement stmt = database.createStatement("SELECT * FROM Session WHERE Timestamp >= " +
					(System.currentTimeMillis() - 7*24*60*60*1000) + " ORDER BY Timestamp desc");
			stmt.prepare();
			c = stmt.getCursor();
			
			Row r;
			Session s;
			int steps, heartrate, avgHeartrate, minHeartrate, maxHeartrate, percentageHRMax;
			float distance, speed, frequency, calories, trimp;
			long duration, timestamp;
			
			while(c.next()){
				r = c.getRow();
				
				timestamp = r.getLong(0);
				steps = r.getInteger(1);
				distance = r.getFloat(2);
				speed = r.getFloat(3);
				heartrate = r.getInteger(4);
				frequency = r.getFloat(5);
				duration = r.getLong(6);
				calories = r.getFloat(7);
				avgHeartrate = r.getInteger(8);
				minHeartrate  = r.getInteger(9);
				maxHeartrate = r.getInteger(10);
				percentageHRMax = r.getInteger(11);
				trimp = r.getFloat(12);
				
				s = new Session(steps, distance, speed, 
						heartrate, frequency, duration, calories, 
						timestamp,avgHeartrate,minHeartrate,maxHeartrate,
						percentageHRMax,trimp);
				
				sessions.addElement(s);
			}
		
			if(sessions.isEmpty())
				return null;
			else
				return sessions;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public static Session getMinimalStepCount(){
		Vector v = claimLastWeeksSessions();
		
		if(v == null){
			return null;
		}else{
			int min = ((Session)(v.elementAt(0))).getSteps();
			int indexMin = 0;
			
			for(int i = 0; i < v.size(); i++){
				Session s = (Session)(v.elementAt(i));
				if(s.getSteps() <= min){
					indexMin = i;
					min = s.getSteps();
				}	
			}
			
			return ((Session)v.elementAt(indexMin));
		}
	}
	
	public static Session getMaximalStepCount(){
		Vector v = claimLastWeeksSessions();
		
		if(v == null){
			return null;
		}else{
			int max = ((Session)(v.elementAt(0))).getSteps();
			int indexMax = 0;
			
			for(int i = 0; i < v.size(); i++){
				Session s = (Session)(v.elementAt(i));
				if(s.getSteps() >= max){
					indexMax = i;
					max = s.getSteps();
				}	
			}
			
			return ((Session)v.elementAt(indexMax));
		}
	}

	public static boolean storeSession(Session s){
		try{
			Statement stmt = database.createStatement("INSERT INTO Session VALUES(" +
				s.getTimeStamp() + "," + s.getSteps() + "," + s.getDistance() + 
				"," + s.getAvgSpeed() + "," + s.getHeartRate() + "," +
				s.getStepFrequency() + "," + s.getDuration() + "," +
				s.getKcalories() + "," + s.getAvgHeartRate() + 
				"," + s.getMinHeartRate() + "," + s.getMaxHeartRate() +
				"," + s.getPercentageOfHRMax() + "," + s.getTrimp() + ")");
			stmt.prepare();
			stmt.execute();
			stmt.close();
			
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
}
